package com.longer.studentmvp.presenter;

import android.os.Handler;
import android.util.Log;

import com.longer.studentmvp.model.bean.ApiInfo;
import com.longer.studentmvp.model.bean.User;
import com.longer.studentmvp.model.bean.WeatherJson;
import com.longer.studentmvp.model.biz.IUserBiz;
import com.longer.studentmvp.model.biz.OnLoginListener;
import com.longer.studentmvp.model.biz.UserBiz;
import com.longer.studentmvp.retrofit.ApiStores;
import com.longer.studentmvp.retrofit.AppClient;
import com.longer.studentmvp.view.iview.IMainActivityView;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
 * Created by longer on 2017/4/5.
 */

public class MainActivityPresenter {

    private IUserBiz userBiz;

    private IMainActivityView userLoginView;

    private Handler mHander = new Handler();

    public MainActivityPresenter(IMainActivityView userLoginView) {
        this.userLoginView = userLoginView;
        this.userBiz = new UserBiz();
    }

    public void login() {
        userLoginView.showLoading();
        userBiz.login(userLoginView.getUserName(), userLoginView.getPassword(), new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {
                mHander.post(new Runnable() {
                    @Override
                    public void run() {
                        userLoginView.toMainActivity(user);
                        userLoginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFailed() {
                mHander.post(new Runnable() {
                    @Override
                    public void run() {
                        userLoginView.showFailedError();
                        userLoginView.hideLoading();
                    }
                });

            }
        });

    }


    public void clear() {
        userLoginView.clearUserName();
        userLoginView.clearPassword();
    }

    public void setheadpicture() {
        userLoginView.setheadpictures();
    }

    public void textrxjava() {
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("rxjava");
                subscriber.onCompleted();
            }
        }).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.d("tip", "完成了");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                Log.d("tip", "onnext:" + s);
            }
        });


        final String[] froms = {"11", "22"};
        Observable.from(froms)
                .subscribe(new Subscriber() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {

                    }

                });

        Observable.just("es", "as")
                .map(new Func1<String, String>() {

                    @Override
                    public String call(String s) {
                        return s + "map";
                    }
                })
                .subscribe(new Subscriber() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {

                    }
                });

        Observable.from(froms)
                .flatMap(new Func1<String, Observable<String>>() {
                    @Override
                    public Observable<String> call(String s) {
                        return Observable.just(s.toUpperCase());
                    }
                })
                .subscribe(new Observer<String>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {

                    }
                });
    }

    public void textretrofit() {
        new Thread() {
            @Override
            public void run() {

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://www.weather.com.cn/")
                        .build();

                ApiStores apiStores = retrofit.create(ApiStores.class);
                Call<ResponseBody> call = apiStores.getWeather("101010100");

                try {
                    Response<ResponseBody> bodyResponse = call.execute();
                    String body = bodyResponse.body().string();
                    Log.d("tip", "同步调用内容：" + body);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                Retrofit retroft = new Retrofit.Builder()
                        .baseUrl("")
                        .build();
                ApiStores apiStores = retroft.create(ApiStores.class);
                Call<ResponseBody> call = apiStores.getWeather("");

                try {
                    Response<ResponseBody> execute = call.execute();
                    String str = execute.body().string();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


        new Thread() {
            @Override
            public void run() {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://www.weather.com.cn/")
                        .build();

                ApiStores apiStores = retrofit.create(ApiStores.class);
                Call<ResponseBody> call = apiStores.getWeather("101010100");
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        try {
                            Log.i("wxl", "异步调用内容：" + response.body().string());
                            userLoginView.showLoading();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable t) {
                        Log.i("wxl", "onFailure=" + t.getMessage());
                    }
                });
            }
        }.start();

    }

    public void getCarType() {
        new Thread() {
            @Override
            public void run() {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://WuXiaolong.me/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                ApiStores apiStores = retrofit.create(ApiStores.class);
                ApiInfo apiInfo = new ApiInfo();
                ApiInfo.ApiInfoBean apiInfoBean = apiInfo.new ApiInfoBean();
                apiInfoBean.setApiKey("666");
                apiInfoBean.setApiName("WuXiaolong");
                apiInfo.setApiInfo(apiInfoBean);
                Call<ResponseBody> call = apiStores.getCarType(apiInfo);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Response<ResponseBody> response) {
                        String body = null;
                        try {
                            body = response.body().string();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Log.d("tip", "get = " + body);
                    }

                    @Override
                    public void onFailure(Throwable t) {

                    }
                });
            }
        }.start();
    }


    public void getWeather() {
//        AppClient.ApiStores apiStores = AppClient.retrofit().create(AppClient.ApiStores.class);
//        Call<WeatherJson> call = apiStores.getWeather("101010100");
//        call.enqueue(new Callback<WeatherJson>() {
//            @Override
//            public void onResponse(Response<WeatherJson> response) {
//                Log.d("tip", "getWeatherinfo=" + response.body().getWeatherinfo().getCity());
//            }
//
//            @Override
//            public void onFailure(Throwable t) {
//
//            }
//        });

        AppClient.ApiStores apiStores = AppClient.retrofit().create(AppClient.ApiStores.class);
        Observable<WeatherJson> Observable = apiStores.getWeatherRxjava("101010100");
        Observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WeatherJson>() {
                    @Override
                    public void onCompleted() {
                        Log.i("wxl", "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("wxl", "e=" + e.getMessage());
                    }

                    @Override
                    public void onNext(WeatherJson weatherJson) {
                        Log.d("tip","getWeather = " + weatherJson.getWeatherinfo().getCity());
                    }
                });

    }
}



















